using System;
using System.Html;
using System.Runtime.CompilerServices;
using Nsb.Classes;
using PlayNexus.Classes;
using Yoshi.Classes;
using jQueryApi;

namespace ThemeChain
{
    public delegate bool ThemeOnceDelegate();

    [IgnoreNamespace]
    public class Theme : ThemeYoshi
    {
        private static readonly bool ThemeInitialized = ThemeOnce();

        /// <summary>
        /// Dynamic overrides performed once upon loading
        /// </summary>
        /// <returns></returns>
        [PreserveName, PreserveCase]
        private static bool ThemeOnce()
        {
            Script.Literal("Theme.ThemeOnce = function(){}");   // Prevent this from happening more than once
            Helpers.StaticInheritClass(typeof (Theme).Name, typeof (ThemeYoshi).Name);

            Window.SetTimeout(
                delegate
                {
                    Inform.Log("Theme.ThemeOnce()");
                    Update();
                    jQuery.Document.Bind("F2Event", FunctionKeys.F2);
                    Master.MasterPage();
                }, 1);

            return !ThemeInitialized; // To make ReSharper happy that ThemeInitialized get's used
        }


        /// <summary>
        /// Reenterant dynamic overrides performed in Master.cs for every new page load
        /// </summary>
        /// <returns></returns>
        [PreserveName, PreserveCase]
#pragma warning disable 108,114
        public static void Update()
#pragma warning restore 108,114
        {
            AppUri = Window.Location.Href.IndexOf("nsb") >= 0 ? AppUri + "NSB" : AppUri;
            PagesDataPath = Uri.Join(AppUri, PagesPartition);
            BgImgUrl = Uri.Versionize("url(" + Uri.Join(PagesDataPath, CurrentPageName, "background.jpg") + ")");
        }
    }
}